﻿using ERP.ERPSystem.WarehouseManagement.Dto;
using ERP.Team.ProductionSystem;
using ERP.Team.WarehouseManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.ERPSystem.WarehouseManagement
{
    /// <summary>
    /// 仓储管理-外协发料-xzc
    /// </summary>
    public class OutsourcedServices : ApplicationService, IOutsourcedServices
    {
        private readonly IRepository<IssuingMaterials, int> _issuingmaterialsRepositroy;
        private readonly IRepository<ProductionWorkOrderMain, int> _productionworkordermainRepositroy;

        public OutsourcedServices(IRepository<IssuingMaterials, int> issuingmaterialsRepositroy, IRepository<ProductionWorkOrderMain, int> productionworkordermainRepositroy)
        {
            _issuingmaterialsRepositroy = issuingmaterialsRepositroy;
            _productionworkordermainRepositroy = productionworkordermainRepositroy;
        }
        /// <summary>
        /// 新增外协发料
        /// </summary>
        /// <param name="issuingmaterialsdto"></param>
        /// <returns></returns>
        public async Task<int> AddIssuingMaterials(IssuingMaterialsDto issuingmaterialsdto)
        {
            var i = ObjectMapper.Map<IssuingMaterialsDto, IssuingMaterials>(issuingmaterialsdto);
            var issuingmaterialsList = await _issuingmaterialsRepositroy.InsertAsync(i);
            return issuingmaterialsList == null ? 0 : 1;
        }
        /// <summary>
        /// 更新外协发料
        /// </summary>
        /// <param name="issuingmaterialsdto"></param>
        /// <returns></returns>
        public async Task<int> UpdateIssuingMaterials(IssuingMaterialsDto issuingmaterialsdto)
        {
            var i = ObjectMapper.Map<IssuingMaterialsDto, IssuingMaterials>(issuingmaterialsdto);
            var issuingmaterialsList = await _issuingmaterialsRepositroy.UpdateAsync(i);
            return issuingmaterialsList == null ? 0 : 1;
        }
        /// <summary>
        /// 删除外协发料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteIssuingMaterials(int id)
        {
            await _issuingmaterialsRepositroy.DeleteAsync(id);
        }
        /// <summary>
        /// 查询外协发料
        /// </summary>
        /// <param name="getissuingmaterialsdto"></param>
        /// <returns></returns>
        public async Task<List<IssuingMaterials>> GetIssuingMaterials(GetIssuingMaterialsDto getissuingmaterialsdto)
        {
            var issuingmaterialsList = await _issuingmaterialsRepositroy.GetListAsync();
            issuingmaterialsList = issuingmaterialsList.WhereIf(!string.IsNullOrEmpty(getissuingmaterialsdto.IssuingCode), m => m.IssuingCode == getissuingmaterialsdto.IssuingCode).ToList();
            issuingmaterialsList = issuingmaterialsList.WhereIf(!string.IsNullOrEmpty(getissuingmaterialsdto.OrderCode), m => m.OrderCode == getissuingmaterialsdto.OrderCode).ToList();
            issuingmaterialsList = issuingmaterialsList.WhereIf(!string.IsNullOrEmpty(getissuingmaterialsdto.SupplierCode),m => m.SupplierCode.Contains(getissuingmaterialsdto.SupplierCode)).ToList();
            issuingmaterialsList = issuingmaterialsList.WhereIf(!string.IsNullOrEmpty(getissuingmaterialsdto.IssuingDate.ToString()), m => m.IssuingDate.ToString() == getissuingmaterialsdto.IssuingDate.ToString()).ToList();
            return issuingmaterialsList;
        }
        /// <summary>
        /// 查询外协工单数据
        /// </summary>
        /// <param name="getproductionworkordermaindto"></param>
        /// <returns></returns>
        public async Task<List<ProductionWorkOrderMain>> GetProductionWorkOrderMain(GetProductionWorkOrderMainDto getproductionworkordermaindto)
        {
            var productionworkordermainList = await _productionworkordermainRepositroy.GetListAsync();
            productionworkordermainList = productionworkordermainList.WhereIf(!string.IsNullOrEmpty(getproductionworkordermaindto.WorkOrderCode), m => m.WorkOrderCode == getproductionworkordermaindto.WorkOrderCode).ToList();
            productionworkordermainList = productionworkordermainList.WhereIf(!string.IsNullOrEmpty(getproductionworkordermaindto.ProductCode), m => m.ProductCode == getproductionworkordermaindto.ProductCode).ToList();
            productionworkordermainList = productionworkordermainList.WhereIf(!string.IsNullOrEmpty(getproductionworkordermaindto.WorkOrderName), m => m.WorkOrderName.Contains(getproductionworkordermaindto.WorkOrderName)).ToList();
            productionworkordermainList = productionworkordermainList.WhereIf(!string.IsNullOrEmpty(getproductionworkordermaindto.ProductName), m => m.ProductName.Contains(getproductionworkordermaindto.ProductName)).ToList();
            productionworkordermainList = productionworkordermainList.WhereIf(!string.IsNullOrEmpty(getproductionworkordermaindto.CustomerName), m => m.CustomerName.Contains(getproductionworkordermaindto.CustomerName)).ToList();
            return productionworkordermainList;
        }
        /// <summary>
        /// 通过id查询到相应的外协发料
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<IssuingMaterials>> FindIssuingMaterials(int id)
        {
            var issuingmaterialsList = await _issuingmaterialsRepositroy.GetListAsync(m=>m.Id == id);
            return issuingmaterialsList;
        }
        /// <summary>
        /// 批量删除外协发料
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> BatchDeleteIssuingMaterials(List<int> ids)
        {
            if (ids == null || ids.Count == 0)
            {
                return 0;
            }

            try
            {
                // 批量删除：根据ID列表删除
                await _issuingmaterialsRepositroy.DeleteManyAsync(ids);
                return ids.Count; // 返回删除的数量
            }
            catch
            {
                return 0; // 删除失败返回0
            }
        }
    }
}
